package zb.wzby;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author wzby
 * @Date: 2022/8/10 19:34
 */
public class Ex3 {
    static CountDownLatch latch = new CountDownLatch(2);

    public static void main(String[] args) {
        for (int i = 1; i < 3; i++) {
            new Thread(() -> {
                String t = Thread.currentThread().getName();
                System.out.printf("%s:开始%n", t);
                Random rand = new Random();
                try {
                    TimeUnit.SECONDS.sleep(rand.nextInt(1, 5));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.printf("%s:结束%n", t);
                latch.countDown();
            }, "go" + i).start();
        }
        try {
            latch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("完成!");
    }
}
